var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../utils/user.js');

import Dialog from '../../../miniprogram_npm/@vant/weapp/dialog/dialog';
// 引入通知框
import Notify from '../../../miniprogram_npm/@vant/weapp/notify/notify';
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast';
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    qiandao_flag:false,
      userInfo: {
        username: '点击登录',
        avatar: '/static/images/avatar.png'
      },
      order: {
        unpaid: 0,
        unship: 0,
        unrecv: 0,
        uncomment: 0
      },
    MyMenus: [
      { url: "/pages/me/collect/collect", pic:"cloud://yizhimao-2g45y.7969-yizhimao-2g45y-1302084299/bs/icon_collect.png",name:"商品收藏"},
      { url: "/pages/me/footprint/footprint", pic: "cloud://yizhimao-2g45y.7969-yizhimao-2g45y-1302084299/bs/footprint.png", name: "浏览足迹" },
      { url: "/pages/me/myGroupon/myGroupon", pic: "cloud://yizhimao-2g45y.7969-yizhimao-2g45y-1302084299/bs/group.png", name: "我的拼团" },
      { url: "/pages/me/address/address", pic: "cloud://yizhimao-2g45y.7969-yizhimao-2g45y-1302084299/bs/address.png", name: "地址管理" },
      { url: "/pages/me/feedback/feedback", pic: "cloud://yizhimao-2g45y.7969-yizhimao-2g45y-1302084299/bs/feedback.png", name: "意见反馈" },
      { url: "/pages/me/about", pic: "cloud://yizhimao-2g45y.7969-yizhimao-2g45y-1302084299/bs/about_us.png", name: "关于我们" }
      // *,{ url: "/pages/about/about", pic: "comment.png", name: "使用帮助" }
      ],
      hasLogin: false,
      totalAmount: 0.00
  },
  // 测试
  context(e){
    console.log(e)
  },
  /**
   * 页面跳转
  */
  goPages:function(e){
    console.log(e);
    if (this.data.hasLogin) {
      wx.navigateTo({
        url: e.currentTarget.dataset.url
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    if(options!=null){
      let parge=options.parge;
      if(parge=='exit'){
        console.log('推出')
        this.exits()
      }
    }
    
    // let userinfo=wx.getStorageSync('userInfo');
    // if(userinfo){
    //   console.log(999);
    //   this.setData({
    //     userInfo:userinfo
    //   })
    // }else{
    //   console.log('用户没有登陆哟');
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },
  onShow:function(){
    let that=this;
    //获取用户的登录信息
    // app.globalData.hasLogin=true;
    // let login=app.globalData.hasLogin
    console.log('app:',app)
    let login=wx.getStorageSync('hasLogin')
    if (login) {
      console.log("我开始获取星系了",app)
      console.log("我开始获取星系了",app.globalData.hasLogin)
      let flag;
      let openid=wx.getStorageSync('openid');
      util.request(api.Update_User,{
        openid:openid
      },"POST").then(res=>{
        if(res.errno==0){
          let flasg=res.flagsing;
          if (flasg==0){
            flag=false
          }else{
            flag=true
          }
            let dbuser=res.data.dbusers[0]
            let remark_count=res.data.remark_count;
            wx.setStorageSync('userInfo', dbuser)
            wx.setStorageSync('remarkcount', remark_count)
            console.log(1)
          }
      })
      // user.update_integral().then(res=>{
      //   if(res.errno==0){
      //   let flasg=res.flagsing;
      //   if (flasg==0){
      //     flag=false
      //   }else{
      //     flag=true
      //   }
      //     let dbuser=res.data.dbusers[0]
      //     let remark_count=res.data.remark_count;
      //     wx.setStorageSync('userInfo', dbuser)
      //     wx.setStorageSync('remarkcount', remark_count)
      //     console.log(1)
      //   }
      // })
      wx.showToast({
        title: '加载中',
        icon:'loading',
        success:res=>{
          setTimeout(() => {
            let userInfo = wx.getStorageSync('userInfo');
            let remarkcount=wx.getStorageSync('remarkcount')
            if(userInfo){
              console.log(2)
              if(userInfo.level==0){
                userInfo.level='普通会员'
              }else if(userInfo.level==1){
                userInfo.level='白银会员'
              }else{
                userInfo.level='黄金会员'
              }
              that.setData({
                userInfo: userInfo,
                remarkcount:remarkcount,
                hasLogin: true,
                qiandao_flag:flag
              });
              console.log(3)
            }else{
              that.setData({
                hasLogin: false
              });
            }
          }, 1000);
        }
      })
     
      
      // util.request(api.GetUserCoupon,{
        util.request(api.PullUserCoupon,{
        openid:wx.getStorageSync('openid'),
      },"POST").then(res=> {
        console.log('res:',res)
        if (res.errno == 0) {
          let count=res.data.usercoupon.length;
          that.setData({
            couponCount: count
          });
        }
      });


    }
  },

  /**
  * 生命周期函数--监听页面卸载
  */
  onUnload: function () {
  
  },
  goLogin() {
    if (!this.data.hasLogin) {
      wx.navigateTo({
        url: "/pages/me/login/login"
      });
    }
  },
  goBrokerage() {
    if (this.data.hasLogin) {
      wx.navigateTo({
        url: "/pages/brokerage/main/main"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  goOrder() {
    if (this.data.hasLogin) {
      try {
        wx.setStorageSync('tab', '0');
      } catch (e) {

      }
      wx.navigateTo({
        url: "/pages/me/order/order"
      });
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    }
  },
  goOrderIndex(e) {
    if (this.data.hasLogin) {
      let tab = e.currentTarget.dataset.index
      let route = e.currentTarget.dataset.route
      try {
        wx.setStorageSync('tab', tab);
      } catch (e) {

      }
      wx.navigateTo({
        url: route,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else {
      wx.navigateTo({
        url: "/pages/auth/login/login"
      });
    };
  },
  goAfterSale: function () {
    wx.showToast({
      title: '目前不支持',
      icon: 'none',
      duration: 2000
    });
  },
  // 推出
  exits(e){
    if(this.data.hasLogin){
      Dialog.alert({
        title: '用户退出',
        message: '你确定要退出吗?',
      }).then(() => {
        if (this.data.hasLogin){
          app.globalData.hasLogin=false;
          wx.setStorageSync('userInfo', '')
          wx.setStorageSync('access_token', '')
          wx.setStorageSync('refresh_token', '')
          wx.setStorageSync('openid', '')
          wx.setStorageSync('hasLogin', 'false')
          wx.setStorageSync('token', '')
          wx.setStorageSync('orderlistin', '')
          user={
            nickName: '点击登录',
            avatarUrl: '/static/images/avatar.png'
          }
        this.setData({
          qiandao_flag:false,
          userInfo:user,
          hasLogin:false,
          
        })}
  
      });
    }else{
      console.log(111111)
      Toast.fail('请登陆哟!');
    }
    
    
    
    // this.onShow()
  },
  toorder(){
    if (this.data.hasLogin){
      wx.switchTab({
        url: '/pages/order/order/order',
      })
    }else{
      Dialog.alert({
        title: '提示',
        message: '用户还没有登陆哟?',
      }).then(() => {
        wx.navigateTo({
          url: '/pages/me/login/login',
        })
    })
    }
  },
  // 提前到
  toqiandao(){
    let that=this;
    util.request(api.Userqiandao,{openid:wx.getStorageSync('openid')},"POST").then(res=>{
      console.log(res)
      if(res.errno==0){
        let flag;
        let flasg=res.flagsing;
        if (flasg==0){
          flag=false
        }else{
          flag=true
        }
        wx.showToast({
          title: res.msg,
          icon:'success'
        })
        let userinfo=that.data.userInfo;
        userinfo['integral']=userinfo['integral']+1
        that.setData({
          userInfo:userinfo,
          qiandao_flag:flag
        })
      }else{
        wx.showToast({
          title: res.msg,
          icon:'error'
        })
      }
    })
  },
  //提示
  tips(){
    wx.showToast({
      title: '你已签到哟！',
      icon:'success'
    })
  },
  // 提转到评论
  toremark(){
    wx.navigateTo({
      url: '/pages/me/myremark/myremark',
    })
  }
})